// 后端接口地址
var baseUrl = "http://ssp-client2.com:9002";
// var baseUrl = "http://ssp-client2.dev33.cn";

var sa = {};

// 打开loading
sa.loading = function(msg) {
	if(window.layer) {
		layer.closeAll();	// 开始前先把所有弹窗关了
		layer.msg(msg, {icon: 16, shade: 0.3, time: 1000 * 20, skin: 'ajax-layer-load' });
	}
};

// 隐藏loading
sa.hideLoading = function() {
	if(window.layer) {
		layer.closeAll();
	}
};

// 封装一下Ajax
sa.ajax = function(url, data, successFn) {
	sa.loading("正在努力加载...");
	setTimeout(function() {
		$.ajax({
			url: baseUrl + url,
			type: "post",
			data: data,
			dataType: 'json',
			headers: {
				'X-Requested-With': 'XMLHttpRequest',
				'satoken': localStorage.getItem('satoken')
			},
			success: function(res){
				console.log('返回数据：', res);
				sa.hideLoading();
				successFn(res);
			},
			error: function(xhr, type, errorThrown){
				sa.hideLoading();
				if(xhr.status == 0){
					return layer.alert('无法连接到服务器，请检查网络');
				}
				return layer.alert("异常：" + JSON.stringify(xhr));
			}
		});
	}, 400);
}


// 从url中查询到指定名称的参数值
function getParam(name, defaultValue){
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if(pair[0] == name){return pair[1];}
	}
	return(defaultValue == undefined ? null : defaultValue);
}
